package controllers

import (
	"encoding/json"
	"errors"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/config"
	"os/exec"
	"strings"
	"syscall"
)

type MainController struct {
	beego.Controller
}
type InfoController struct {
	beego.Controller
}
type user struct {
	username string
	passwd   string
}

type LoadStatus struct {
	Now  string `json:"now"`
	Bf5  string `json:"bf5"`
	Bf15 string `json:"bf15"`
}

type MemStatus struct {
	All  uint64 `json:"all"`
	Used uint64 `json:"used"`
	Free uint64 `json:"free"`
}

type Disksingle struct {
	All  uint64 `json:"all"`
	Used uint64 `json:"used"`
	Free uint64 `json:"free"`
}
type DiskStatus struct {
	Root Disksingle
	Data Disksingle
}
type WjController struct {
	beego.Controller
}

func (w *WjController) Get() {
	w.TplNames = "wj.html"
}

func (this *MainController) Get() {
	v := this.GetSession("username")
	if v != nil {
		this.TplNames = "chart.html"
	} else {
		this.TplNames = "index.html"
	}
}
func (m *MainController) Post() {
	username := m.Input().Get("username")
	if strings.EqualFold(username, "") {
		m.TplNames = "index.html"
	}
	passwd := m.Input().Get("pwd")
	if strings.EqualFold(passwd, "") {
		m.TplNames = "index.html"
	}
	us, err := auth(username)
	if err != nil {
		m.TplNames = "index.html"
	}

	if strings.EqualFold(us.passwd, passwd) {
		m.SetSession("username", username)
		m.SetSession("passwd", passwd)
		m.TplNames = "chart.html"
	} else {
		m.TplNames = "index.html"
	}
}

func (i *InfoController) Mem() {
	s := MemStat()
	aa, _ := json.Marshal(s)
	i.Ctx.WriteString(string(aa))

}
func (i *InfoController) Disk() {
	s := DiskUsage("/")
	k := DiskUsage("/home")
	dis := DiskStatus{s, k}
	aa, _ := json.Marshal(dis)
	i.Ctx.WriteString(string(aa))
}
func (i *InfoController) Load() {
	rs := loadstat()
	bb, err := json.Marshal(rs)
	if err != nil {
		i.Ctx.WriteString("转化失败")
	} else {
		i.Ctx.WriteString(string(bb))
	}

}

func auth(use string) (user, error) {
	var u user
	userconf, err := config.NewConfig("ini", "auth")
	if err != nil {
		return user{}, errors.New("提取文件失败")
	}
	u.username = use
	u.passwd = userconf.String(use)
	if u.passwd != "" {
		return user{}, errors.New("未找到密码")
	}
	return u, nil

}

func MemStat() MemStatus {
	mem := MemStatus{}

	sysInfo := new(syscall.Sysinfo_t)
	err := syscall.Sysinfo(sysInfo)
	if err == nil {
		mem.All = sysInfo.Totalram / 1024 / 1024 //* uint64(syscall.Getpagesize()) / 1024 / 1024 / 1024
		mem.Free = sysInfo.Freeram / 1024 / 1024 //* uint64(syscall.Getpagesize()) / 1024 / 1024 / 1024
		mem.Used = mem.All - mem.Free
	}
	return mem
}

func DiskUsage(path string) (disk Disksingle) {
	fs := syscall.Statfs_t{}
	err := syscall.Statfs(path, &fs)
	if err != nil {
		return
	}
	disk.All = fs.Blocks * uint64(fs.Bsize) /// 1024 // 1024
	disk.Free = fs.Bfree * uint64(fs.Bsize) /// 1024 // 1024
	disk.Used = disk.All - disk.Free
	return
}
func loadstat() LoadStatus {
	bb := LoadStatus{}
	binpath, _ := exec.LookPath("w")
	cmd := exec.Command(binpath)
	out, _ := cmd.Output()
	a := strings.Split(string(out), "\n")[0]
	s := strings.Split(a, ",")
	bb.Bf15 = s[len(s)-1]
	bb.Bf5 = s[len(s)-2]
	bb.Now = strings.Split(s[len(s)-3], ":")[1]
	return bb
}
